#region using

using System.Web.UI.WebControls;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

#endregion

namespace PS.WebParts.Rollup.GridViewWebPart
{
    internal class ToolPartBuilder
    {
        private ToolPart        owner;
        private int             buttonCounter = 0;
        private string          editButtonSourceViewEventHandler;

        private static string   ToolpartQualifier = "PSGridViewDataFormToolpartProp";

        public ToolPartBuilder(ToolPart owner)
        {
            this.owner = owner;

            editButtonSourceViewEventHandler =
                string.Format("javascript:MsoContentToolpartBasicEdit('{0}',{{0}})",
                    SPHttpUtility.EcmaScriptStringLiteralEncode(SPHttpUtility.UrlPathEncode("/_layouts/zoombldr.aspx", true)));

            ScriptLink.Register(owner.Page, "ctp.js", true);
        }

        private string CreateEditButton(string buttonText, string textID)
        {
            string result = ("<input id=\"" + textID + "Button" +
                 "\" type=\"button\" class=\"UserButton\" style=\"width:" +
                 SPHttpUtility.EcmaScriptStringLiteralEncode("150") + "px;\" value=\""
                 + SPHttpUtility.HtmlEncode(buttonText) + "\" onclick=\"" +
                 SPHttpUtility.NoEncode(string.Format(editButtonSourceViewEventHandler, textID, "ltr")) + "\" />");


            return result;
        }

        public string CreateToolText(string headerText, string description, string buttonText)
        {
            string textID = ToolpartQualifier + (buttonCounter++);

            string toolText =
                @"
                <div class='UserGeneric'>
                    <div class='UserSectionHead ms-bold'>{0}</div>
                    <div class='UserSectionBody'>
                        {1}<br/>
                        <div class='UserControlGroup'>{2}<br/></div>
                    </div>
                </div>";

            string buttonCode = CreateEditButton(buttonText, textID);

            string text = string.Format(toolText, SPHttpUtility.HtmlEncode(headerText),
                           SPHttpUtility.HtmlEncodeAllowSimpleTextFormatting(description),
                           SPHttpUtility.NoEncode(buttonCode));

            AddLiteralControl(text);

            return textID;
        }

        private void AddLiteralControl(string text)
        {
            Literal literal = new Literal();
            literal.Text = text;
            owner.Controls.Add(literal);
        }

        public void CreateButton(string text, System.EventHandler handler )
        {
            Button button = new Button();
            button.Text = text;
            button.Click += handler;
            AddLiteralControl("<table width='100%'><tr><td><div class='UserDottedLine' style='WIDTH: 100%' /></td></tr><tr><td align='center'>");
            owner.Controls.Add(button);
            AddLiteralControl("</td></tr></table>");
        }

        public void RegisterField(string field, string initialValue)
        {
            owner.Page.ClientScript.RegisterHiddenField(field, SPHttpUtility.NoEncode(initialValue));
        }
        public string RetrieveValue(string field)
        {
            return owner.Page.Request.Form[field];
        }
    }
}
